<template>
    <div>
        <el-row :gutter="20">
          <el-col :span="12">
              <el-card>
                <div slot="header" class="clearfix">
                    <span>公共参数列表</span>
                </div>
                <el-form :model="queryParamsLeft" ref="queryParamsLeft" :inline="true" label-width="100px">
                        <el-form-item label="字典编号：" prop="code">
                            <el-input v-model="queryParamsLeft.code" placeholder="字典编号" size="small"></el-input>
                        </el-form-item>
                        <el-form-item label="字典名称：" prop="dictValue">
                            <el-input v-model="queryParamsLeft.dictValue" placeholder="字典名称" size="small"></el-input>
                        </el-form-item>
                        <el-row>
                          <el-col :span="24"  style="text-align: right">
                            <el-form-item>
                                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQueryLeft">搜索</el-button>
                                <el-button icon="el-icon-delete" size="small" @click="resetQueryLeft('queryParamsLeft')">清空</el-button>
                            </el-form-item>
                          </el-col>
                        </el-row>
                        
                </el-form>
                <el-row :gutter="10">
                    <el-col :span="24">
                        <div style="margin-bottom: 10px">
                            <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                            <el-button type="danger" plain icon="el-icon-delete" size="small" @click="handleDelete($refs.tablRightRef.selection)">删除</el-button>
                        </div>
                    </el-col>
                </el-row>
                <el-table :data="tableDataLeft" ref="tablRightRef" style="width: 100%" row-key="id" border size="small" @row-click="handleRowClick">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column type="index" label="#" width="50"></el-table-column>
                    <el-table-column prop="code" label="字典编号"></el-table-column>
                    <el-table-column prop="dictValue" label="字典名称"></el-table-column>
                    <el-table-column prop="isSealed" label="封存">
                        <template slot-scope="{row}">
                            <el-tag type="primary" v-if="row.isSealed===1">是</el-tag>
                            <el-tag type="primary" v-else>否</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope"> 
                            <el-button type="text" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
                            <el-button size="mini" type="text" @click="handleDel(scope.row.id)" icon="el-icon-delete">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    v-show="totalLeft>0"
                    @size-change="handleSizeChangeLeft"
                    @current-change="handleCurrentChangeLeft"
                    background
                    :current-page="1"
                    :page-sizes="[10, 20, 30, 40,50,100]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    class="pagination-container"
                    :total="totalLeft">
                </el-pagination>
              </el-card>
          </el-col>
           <el-col :span="12">
              <el-card>
                <div slot="header" class="clearfix">
                    <span>[{{rightTitle}}]公共参数详情</span>
                </div>
                <el-form :model="queryParamsRight" ref="queryParamsRight" :inline="true" label-width="100px">
                    <el-form-item label="字典编号：" prop="code">
                        <el-input v-model="queryParamsRight.code" placeholder="字典编号" size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="字典名称：" prop="dictValue">
                        <el-input v-model="queryParamsRight.dictValue" placeholder="字典名称" size="small"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="24"  style="text-align: right">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="small" @click="handleQueryLeft">搜索</el-button>
                            <el-button icon="el-icon-delete" size="small" @click="resetQueryLeft('queryParamsLeft')">清空</el-button>
                        </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <el-row :gutter="10">
                    <el-col :span="24">
                        <div style="margin-bottom: 10px">
                            <el-button type="primary" icon="el-icon-plus" size="small" @click="handleRightAdd">新增</el-button>
                            <el-button type="danger" plain icon="el-icon-delete" size="small" @click="handleDelete($refs.tableRightRef.selection)">删除</el-button>
                        </div>
                    </el-col>
                </el-row>
                <el-table :data="tableDataRight" ref="tableRightRef" style="width: 100%" row-key="id" border size="small">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column type="index" label="#" width="50"></el-table-column>
                    <el-table-column prop="code" label="字典编号"></el-table-column>
                    <el-table-column prop="dictValue" label="字典名称"></el-table-column>
                    <el-table-column prop="isSealed" label="封存">
                        <template slot-scope="{row}">
                            <el-tag type="primary" v-if="row.isSealed===1">是</el-tag>
                            <el-tag type="primary" v-else>否</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope"> 
                            <el-button type="text" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
                            <el-button size="mini" type="text" @click="handleDelete(scope.row.id)" icon="el-icon-delete">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    v-show="totalRight>0"
                    @size-change="handleSizeChangeLeft"
                    @current-change="handleCurrentChangeLeft"
                    background
                    :current-page="1"
                    :page-sizes="[10, 20, 30, 40,50,100]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    class="pagination-container"
                    :total="totalRight">
                </el-pagination>
              </el-card>
          </el-col>
        </el-row>
        <el-dialog :title="title" :visible.sync="dictModal" width="50%">
                <el-form :model="dictbizForm" :rules="dictbizRules" ref="dictbizForm">
                     <el-row :gutter="20">
                        <el-col :span="24">
                            <el-form-item  prop="code" label="字典编号：" :label-width="formLabelWidth" required>
                                <el-input v-model="dictbizForm.code" autocomplete="off" size="mini" placeholder="请输入字典编号"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item  prop="dictValue" label="字典名称：" :label-width="formLabelWidth" required>
                                <el-input v-model="dictbizForm.dictValue" autocomplete="off" size="mini" placeholder="请输入字典名称"></el-input>
                            </el-form-item>
                        </el-col>
                         <el-col :span="12">
                            <el-form-item  prop="dictValue" label="上级字典：" :label-width="formLabelWidth" required>
                                <el-select v-model="dictbizForm.parentId" placeholder="请选择" size="small" disabled>
                                    <el-option :label="dictbizForm.$parentId" :value="dictbizForm.parentId"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item  prop="dictKey" label="字典键值：" required v-if="mode==='subAdd' || mode==='subEdit'">
                                <el-input v-model="dictbizForm.dictKey" size="mini" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                         <el-col :span="12">
                            <el-form-item  prop="sort" label="字典排序：" :label-width="formLabelWidth" required>
                                 <el-input-number v-model="dictbizForm.sort" size="mini" style="width: 100%" controls-position="right"></el-input-number>
                            </el-form-item>
                         </el-col>
                         <el-col :span="12">
                           <el-form-item  prop="isSealed" label="封存：" :label-width="formLabelWidth" required>
                                <el-switch
                                    v-model="dictbizForm.isSealed"
                                    active-text="是"
                                    inactive-text="否">
                                </el-switch>
                            </el-form-item>
                      </el-col>
                       <el-col :span="12">
                           <el-form-item  prop="remark" label="字典备注：" :label-width="formLabelWidth">
                               <el-input v-model="dictbizForm.remark" autocomplete="off" size="mini" placeholder="请输入字典备注"></el-input>
                            </el-form-item>
                      </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="handleSubmit('dictbizForm')" size="small">保存</el-button>
                    <el-button @click="dictModal = false" size="small">取消</el-button>
                </div>
         </el-dialog>
    </div>
</template>
<script>
import { saveDictbizForm,getDictParentList,getDictChildList,remove } from '@/api/system/dictbiz.api.js'
    export default {
        data() {
            return {
                totalLeft: 0,
                queryParamsLeft: {
                    current: 1,
                    size: 10,
                    code: undefined,
                    dictValue: undefined
                },
                totalRight: 0,
                queryParamsRight: {
                    current: 1,
                    size: 10,
                    code: undefined,
                    dictValue: undefined
                },
                tableDataLeft: [],
                tableDataRight: [],
                dictModal: false,
                dictbizForm: {
                    isSealed: false
                },
                formLabelWidth:"100px",
                dictbizRules: {
                    code: [
                        { required: true, message: '请输入字典编号', trigger: 'blur' },
                    ],
                    dictValue: [
                        { required: true, message: '请输入字典名称', trigger: 'blur' },
                    ],
                    isSealed: [
                        { required: true, message: '请选择封存', trigger: 'blur' },
                    ],
                    sort: [
                        { required: true, message: '请输入字典排序', trigger: 'blur' },
                    ],
                },
                title: '新增',
                rightTitle: '暂无',
                mode: 'addLeft'
            }
        },
        mounted() {
            this.queryParamsRight.parentId = -1;
            this.getList();
            this.getSubList();
        },
        methods: {
            handleRightAdd() {
                this.mode = 'addLeft';
                this.dictModal = true;
            },
            async getList() {
                const result = await getDictParentList(this.queryParamsLeft);
                if(result.code === 200) {
                    this.tableDataLeft = result.data.records;
                    this.totalLeft = result.data.total;
                }
            },
            async getSubList() {
                const result = await getDictChildList(this.queryParamsRight);
                if(result.code === 200) {
                    this.tableDataRight = result.data.records;
                    this.totalRight = result.data.total;
                }
            },
            handleSizeChangeLeft(val) {
                this.queryParamsLeft.size = val;
                this.getList();
            },
            handleCurrentChangeLeft(val) {
                this.queryParamsLeft.current = val;
                this.getList();
            },
               // 搜索
            handleQueryLeft() {
                this.getList();
            },
            // 清空
            resetQueryLeft(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleAdd() {
                this.dictModal = true;
            },
            handleSubmit(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        this.dictbizForm.isSealed?this.dictbizForm.isSealed=1:this.dictbizForm.isSealed=0
                        this.dictbizForm.isSeale?this.dictbizForm.$isSealed='是':this.dictbizForm.$isSealed='否'
                        this.dictbizForm.isSealed === 0?this.dictbizForm.$isSealed = '否':'是'
                           const result =  await saveDictbizForm(this.dictbizForm)
                            if(result.code === 200) {
                                this.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                                this.dictModal = false;
                            }
                    } else {
                        return false;
                    }
                });
            },
            async handleRowClick(row) {
              this.queryParamsRight.parentId = row.id
              this.rightTitle = row.code
              this.getSubList();
            },
            handleEdit(row) {
                this.dictModal = true;
                this.mode = 'editRight';
                this.title = '编辑';
                this.dictbizForm = row;
            },
            handleDelete(ids) {
                let deptIds;
                typeof ids === 'string'?deptIds=ids:deptIds=ids.map(item=>item.id).toString();
                if(deptIds.length===0) {
                    this.$message({
                        type: 'error',
                        message: '请至少选择一条数据'
                    })
                    return;
                }
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(deptIds);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                          this.getList();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>